package com.gopher.gauth.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GAuthRealm extends AuthorizingRealm {
    private static final Logger LOG = LoggerFactory.getLogger(GAuthRealm.class);


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        String username =(String) principals.getPrimaryPrincipal();
        LOG.info("即将为给用户:{}添加权限",username);
        authorizationInfo.addStringPermission("account_add");
        return authorizationInfo;
    }


    /**
     * 对登陆用户进行认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        SimpleAuthenticationInfo info = null;
        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
        String username = upToken.getUsername();
        char[] password = upToken.getPassword();
        LOG.info("用户名----->" + username);
        LOG.info("用户密码----->" + password);
        info = new SimpleAuthenticationInfo(
                username, //用户名
                password, //密码
                null, //加密的盐值
                getName()  //realm name
        );
        return info;
    }
}
